<template>

	<view class="cart" v-if="cart.length !== 0">
		<!-- 收货地址组件 -->
		<my-address></my-address>
		<!-- 标题 -->
		<view class="cart-title">
			<uni-icons type="shop" size="20"></uni-icons>
			<text>购物车</text>
		</view>
		<!-- 主体部分 -->
		<view class="cart-list">
			<uni-swipe-action>
				<block v-for="(item,index) in cart" :key="index">
					<uni-swipe-action-item :right-options="options" @click="swiperItemClickHandler(item)">
						<view class="cart-list-item">
							<!-- 勾选状态 -->
							<view class="cart-checked">
								<radio value="" :checked="item.goods_state" color="#c00000"
									@click="radioChange(item)" />
							</view>
							<!-- 图片区域 -->
							<view class="cart-img">
								<image :src="item.goods_small_logo"></image>
							</view>
							<!-- 信息区域 -->
							<view class="cart-info">
								<!-- 商品名称 -->
								<view class="cart-name">
									{{item.goods_name}}
								</view>
								<!-- 商品数量和价格 -->
								<view class="cart-number">
									<view class="cart-price">
										￥{{item.goods_price}}
									</view>
									<view class="cart-count">
										<uni-number-box :min="1" :max="99" :value="item.goods_count"
											@change="(value)=>changValue(value,item.goods_id)"></uni-number-box>
									</view>
								</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
		</view>
		<!-- 使用自定义的结算组件 -->
		<my-settle></my-settle>
	</view>
	<view class="cartbg" v-else>
			<image src="../../static/cart.jpeg"></image>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import badgeMix from '@/mixin/tabbar-badge.js'
	export default {
		computed: {
			...mapState('m_cart', ['cart'])
		},
		mixins: [badgeMix],
		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#c00000'
					}
				}]
			};
		},
		methods: {
			// 修改勾选状态
			radioChange(item) {
				// console.log(item)
				const goods = {
					goods_id: item.goods_id,
					goods_state: !item.goods_state
				};
				// console.log(goods)
				this.updateState(goods)
			},
			...mapMutations('m_cart', ['updateState', 'updateCount', 'removeGoodsById']),

			// input值发生变化时
			changValue(newVal, id) {
				if (Number(newVal) && String(newVal).indexOf('.') == -1) {
					const goods = {
						goods_id: id,
						goods_count: newVal
					}
					this.updateCount(goods);
				}
			},
			// 点击滑块删除时触发
			swiperItemClickHandler(item) {
				// console.log(item)	
				this.removeGoodsById(item.goods_id)
			}
		}

	}
</script>

<style lang="scss">
	.cart {
		background-color: #fff;
	}

	.cartbg{
		width: 100%;
		height: 100vh;
		image{
			width: 100%;
			height: 100%;
		}
	}
	.cart-title {
		height: 40px;
		display: flex;
		justify-content: flex-start;
		padding: 8px 5px;
		border-bottom: 1px solid #efefef;
		align-items: center;
		position: relative;
		font-size: 14px;

		.uni-icons {
			padding-right: 10px;
		}
	}

	.cart-list {
		display: flex;
		flex-direction: column;
	}

	.cart-list-item {
		display: flex;
		align-items: center;
		justify-content: space-around;
		border-bottom: 1px solid #efefef;
		padding: 10px 0;

		.cart-img {
			width: 100px;
			height: 80px;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.cart-info {
			display: flex;
			justify-content: space-between;
			height: 100px;
			width: 200px;
			flex-direction: column;

			.cart-name {
				font-size: 12px;
			}

			.cart-number {
				display: flex;
				justify-content: space-between;
				height: 30px;
				width: 180px;
				align-items: center;

				.cart-price {
					color: #c00000;
				}

			}
		}
	}
</style>
